package com.lxy.新特性.lambda;

import com.lxy.mybatis.pojo.User.Emp;
import com.lxy.mybatis.pojo.User.EnumStatus;
import com.lxy.新特性.策略设计模式.comm;
import com.lxy.新特性.策略设计模式.imp.ageWhere;
import com.lxy.新特性.策略设计模式.nums;
import org.junit.Test;

import java.util.*;

public class LambdaTest{
    /**
     * 匿名类部类：每实现一个功能都要实现一个方法
     * 策略设计模式：
     * lambda
     * Stream*/
    @Test
    public void test(){
        //比较大小
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //功能：从小到大进行排序
                return o1.compareTo(o2);
            }
        };

        Set<Integer> set = new TreeSet<>();
        set.add(3);
        set.add(1);
        set.add(2);

        for(Integer item : set){
            System.out.println(item);
        }
    }

    //初始化员工数据
    List<Emp> tmp = Arrays.asList(
            new Emp(1,"lxy",2,'女',"123@qq.com", EnumStatus.CLOSE),
            new Emp(2,"lili",21,'女',"456@qq.com", EnumStatus.OPEN),
            new Emp(3,"lucy",21,'女',"789@qq.com", EnumStatus.CLOSE),
            new Emp(4,"pite",41,'男',"101@qq.com", EnumStatus.OPEN),
            new Emp(4,"pite",41,'男',"101@qq.com",EnumStatus.FREE),
            new Emp(4,"pite",41,'男',"101@qq.com",EnumStatus.FREE),
            new Emp(4,"pite",41,'男',"101@qq.com",EnumStatus.OPEN),
            new Emp(8,"qiqi",1,'女',"110@qq.com",EnumStatus.CLOSE)
    ) ;

    //初始化过滤功能方法
    public List<Emp> filterDate (List<Emp> score, comm<Emp> c){
        List<Emp> empNew = new ArrayList<>();
        for (Emp emp : score) {
            //两个实现类的同名方法
            if(c.target(emp)){
                empNew.add(emp);
            }
        }
        return empNew;
    }

    @Test
    public void test1(){
        List list = filterDate(tmp, new ageWhere()); //对应功能的实现方法
        for (Object o : list) {
            System.out.println(o);
        }
    }

    @Test
    public void test2(){
        List list = filterDate(tmp, new comm<Emp>() {
            @Override
            public boolean target(Emp methods) {
                return methods.getSex() == '女';
            }
        });

        for (Object o : list) {
            System.out.println(o);
        }
    }

    @Test
    public void test3(){
        //在匿名内部类中使用同级别参数用final修饰
        final Integer num = 1;
        List list = filterDate(tmp, a ->a.getAge()>20 );
        List list1 = filterDate(tmp, (Emp a) ->{
            System.out.println("lambda中多条语句加花括号"+num);
            return a.getAge()>20;
        } );
//        list.forEach(System.out::println);
    }

    @Test
    public void test4(){
        tmp.stream().filter(emp -> emp.getSex() == '女').limit(2).forEach(System.out::println);
        tmp.stream().map(emp -> emp.getEmpName()).forEach(System.out::println);
    }

    @Test
    public void test5(){
        Integer add = methods(100, 200, (a, b) -> a + b);
        System.out.println(add);
    }

    //方法作为函数式接口的容器，调用
    public Integer methods(Integer num1, Integer num2, nums tmp){
        return tmp.getNum(num1,num2);
    }
}
